import Vue from 'vue'
import Router from 'vue-router'
import home from '../views/Home.vue'
import welcome from '../components/welcome/Welcome.vue'
import user from '../components/User/User.vue'

//资产入库模块
import Asset from '../components/Asset/Asset.vue'
import AssetList from '../components/Asset/AssetList.vue'
import AssetNowAdd from '../components/Asset/AssetNowAdd.vue'
import AssetAdd from '../components/Asset/AssetAdd.vue'
import CustomModule from '../components/Asset/CustomModule.vue'

//统计分析模块
import Statistics from '../components/Statistics/Statistics.vue'
import StatisticsList from '../components/Statistics/StatisticsList.vue'
import Department from '../components/Statistics/Department.vue'
import Subtotal from '../components/Statistics/Subtotal.vue'
import Classification from '../components/Statistics/Classification.vue'

//盘点管理模块
import Check from '../components/Check/Check.vue'
import InventoryData from '../components/Check/InventoryData.vue'
import CountingTask from '../components/Check/CountingTask.vue'

//标签模块
import Board from '../components/Board/Board.vue'
import TagModule from '../components/Board/TagModule.vue'
import AssetModule from '../components/Board/AssetModule.vue'

Vue.use(Router)

let router = new Router({
    routes: [
        {
            path: '/Login',
            name: 'Login',
            component: function () {
                return import('../views/Login.vue')
            }
        }, 
        {
            path: '/ForgetPassword',
            name: 'ForgetPassword',
            component: function () {
                return import('../components/Forget/ForgetPassword.vue')
            }
        },
        {
            path: '/Register',
            name: 'Register',
            component: function () {
                return import('../views/Register.vue')
            }
        },
        {
            path: '/',
            name: 'Home',
            component: home,
            redirect: {
                path: '/welcome'
            },
            children: [{
                name: 'welcome',
                path: '/welcome',
                component: welcome
            },
            {
                path: '/User',
                name: 'user',
                component: user
            },
            {
                path: '/Asset',
                name: 'Asset',
                component: Asset,
                redirect: {
                    path: '/AssetList'
                },
                /* path: '/Asset',
                name: 'Asset',
                component: function () {
                    return import('../components/Asset/Asset.vue')
                } */
                children: [{
                    name: 'AssetList',
                    path: '/AssetList',
                    component: AssetList
                },
                {
                    name: 'AssetNowAdd',
                    path: '/AssetNowAdd',
                    component: AssetNowAdd
                },
                {
                    name: 'AssetAdd',
                    path: '/AssetAdd',
                    component: AssetAdd
                },
                {
                    name: 'CustomModule',
                    path: '/CustomModule',
                    component: CustomModule
                },
                ],
            },
            {
                path: '/Statistics',
                name: 'Statistics',
                component: Statistics,
                redirect: {
                    path: '/StatisticsList'
                },
                /* component: function () {
                    return import('../components/Statistics/Statistics.vue')
                }, */
                children: [{
                    name: 'StatisticsList',
                    path: '/StatisticsList',
                    component: StatisticsList
                },
                {
                    name: 'Department',
                    path: '/Department',
                    component: Department
                },
                {
                    name: 'Subtotal',
                    path: '/Subtotal',
                    component: Subtotal
                },
                {
                    name: 'Classification',
                    path: '/Classification',
                    component: Classification
                },
                ],
            },
            {
                path: '/Check',
                name: 'Check',
                component: Check,
                redirect: {
                    path: '/InventoryData'
                },
                /*  component: function () {
                     return import('../components/Check/Check.vue')
                 } */
                children: [{
                    name: 'InventoryData',
                    path: '/InventoryData',
                    component: InventoryData
                },
                {
                    name: 'CountingTask',
                    path: '/CountingTask',
                    component: CountingTask
                }
                ],
            },
            {
                path: '/Board',
                name: 'Board',
                component: Board,
                redirect: {
                    path: '/TagModule'
                },
                /* component: function () {
                    return import('../components/Board/Board.vue')
                }, */
                children: [{
                    name: 'TagModule',
                    path: '/TagModule',
                    component: TagModule
                },
                {
                    name: 'AssetModule',
                    path: '/AssetModule',
                    component: AssetModule
                }
                ],
            },
            ]
        },
        {
            path: '/Notice',
            name: 'Notice',
            component: function () {
                return import('../components/Notice/Notice.vue')
            }
        },
    ]
})
//注册一个全局守卫,作用是在路由跳转前,对路由进行判断,防止未登录的用户跳转到其他需要登录的页面去
router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('mytoken')
    //如果登录,放行
    if (token) {
        next()
    } else {
        //如果没有登录.访问非登录页面,则跳转到登录页面
        if (to.path != '/Login') {
            next({
                path: '/Login'
            })
            next();
        }
        else {
            //如果没有登录,但访问的是登录页面,直接进入
            next()
        }
    }
})

export default router